﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Collections.Generic;

namespace BSPortal.Usercontrol.Admin.Book.HTML
{
    public partial class ListHTMLContent : System.Web.UI.UserControl
    {
        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    var data = mainService.GetListAllCategoryByParent(1);
        //    GridView1.DataSource = data;
        //    GridView1.DataBind();
        //}
        int _pageIndex = 1;
        MainService service = new MainService();
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!CommonWeb.CheckLogin())
            //    Response.Redirect("~/Login.aspx");

            //if (!IsPostBack)
            //{
            //    rdToDate.SelectedDate = DateTime.Today;
            //    rdFromDate.SelectedDate = DateTime.Today.AddDays(-10);
            //    LoadProduct();
            //    LoadCity();
            //    LoadPriceType();
            LoadData();
            //}
        }

        #region Load Data
        void LoadData()
        {
            bindData(txtName.Text.Trim());
        }
        #endregion

        #region Grid Event
        protected void bindData(string likeName)
        {
            try
            {
                List<BSPortal.HTMLContent> listHTMLContent=null;
                if (likeName == "")
                    listHTMLContent = service.GetAllHTMLContent();
                else
                    listHTMLContent = service.GetListHTMLContentConditionLikeName(likeName);
                RadGrid.DataSource = new object[0];
                RadGrid.VirtualItemCount = 0;
                RadGrid.DataBind();

                RadGrid.DataSource = listHTMLContent;
                RadGrid.VirtualItemCount = listHTMLContent.Count;
                RadGrid.PageSize = PageConst.PAGES_SIZE;
                RadGrid.DataBind();

            }
            catch (Exception exception)
            {
            }

        }

        protected void RadGrid_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
        {
            _pageIndex = e.NewPageIndex + 1;
        }

        protected void RadGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                if ((e.Item is GridEditableItem && e.Item.IsInEditMode))//for remove duplicate caption and edit form template
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    try
                    {
                        item.Controls[1].Controls[0].Controls[1].Controls[0].Controls[0].Visible = false;
                    }
                    catch
                    {
                    }
                }

                if (e.Item is GridDataItem)//for item template
                {
                    if (e.Item is GridDataItem)//for item template
                    {
                        Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                        if (lblSTT != null)
                            lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                    }

                    Image imgUpdate = (Image)e.Item.FindControl("imgUpdate");
                    Image imgDelete = (Image)e.Item.FindControl("imgDelete");

                    string Id = ((BSPortal.HTMLContent)e.Item.DataItem).ID.ToString();
                    string script = string.Format("document.getElementById('{0}').value='{1}'; return true;", hdId.ClientID, Id);
                    string script2 = string.Format("document.getElementById('{0}').value='{1}'; if(confirm('Xóa nội dung này ?')) return true; else return false;", hdId.ClientID, Id);
                    imgUpdate.Attributes.Add("onclick", script);
                    imgDelete.Attributes.Add("onclick", script2);
                }
            }
            catch (Exception)
            {
            }
        }

        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            bindData(txtName.Text.Trim());
        }

        protected void imgAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            RadWindow frmProductPriceNewsWindow = frmHTMLContent;
            frmHTMLContent.Height = 650;
            frmHTMLContent.Title = "Thêm nội dung";
            frmProductPriceNewsWindow.NavigateUrl = "frmListHTMLContent.aspx";
            frmProductPriceNewsWindow.Visible = true;
            frmProductPriceNewsWindow.Width = 800;
            frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }

        protected void imgUpdate_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            RadWindow frmProductPriceNewsWindow = frmHTMLContent;
            frmHTMLContent.Height = 650;
            frmProductPriceNewsWindow.Title = "Chỉnh sửa nội dung";
            frmProductPriceNewsWindow.NavigateUrl = "frmListHTMLContent.aspx?HTMLContentId=" + hdId.Value;
            frmProductPriceNewsWindow.Visible = true;
            frmProductPriceNewsWindow.Width = 800;
            frmProductPriceNewsWindow.KeepInScreenBounds = false;
            frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }

        protected void imgDelete_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            MainService service = Common.GetClientService();
            int retcode = service.DeleteHTMLContent(short.Parse(hdId.Value));
            if (retcode == (int)Common.RetCode.SUCESS)
            {
                bindData(txtName.Text.Trim());
            }
        }
        #endregion

        #region Click
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            RadGrid.CurrentPageIndex = 0;
            _pageIndex = 1;
            bindData(txtName.Text.Trim());
        }
        #endregion   
    }
}